package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.util.Const;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie工具类
 */
public class CookieUtil {
    //保存cookie到浏览器
    public static void addCookie(HttpServletResponse resp, String userInfoCookieName, String token) {
        Cookie cookie = new Cookie(userInfoCookieName, token);
        cookie.setPath("/"); //所有路径都可以使用该cookie
        cookie.setMaxAge(Const.USERINFO_TIMEOUT * 60);
        resp.addCookie(cookie);
    }

    public static Cookie getCookie(HttpServletRequest request, String userInfoCookieName) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals(userInfoCookieName)){
                    return cookie;
                }
            }
        }
        return null;
    }
}
